﻿using System;
using Lucene.Net.Analysis;
using System.Text;
using System.IO;
using TermAttribute = Lucene.Net.Analysis.Tokenattributes.TermAttribute;
using Version = Lucene.Net.Util.Version;
using System.Collections.Generic;

namespace OMC.DataManagement
{
    /// <summary>
    /// this class could use the lucene methods to analyze text
    /// </summary>
    class LuceneAnalyzer : IDisposable
    {
        private Analyzer TheAnalyzer;
        private Analyzer TheAnalyzerWithGermanStopWords;
        private TokenStream TheTokenStream;
        private StringBuilder TheStringBuilder;
        private Version TheVersion;
        private static readonly List<String> STOP_WORDS = new List<string>();
        private static readonly List<String> STOP_WORDS_GERMAN = new List<string>();

        /// <summary>
        /// constructor of the LuceneAnalyzer
        /// </summary>
        public LuceneAnalyzer()
        {
            buildStoppWordList();
            TheVersion = new Version("LUCENE",35);
            TheAnalyzerWithGermanStopWords = new Lucene.Net.Analysis.Standard.StandardAnalyzer(TheVersion, STOP_WORDS_GERMAN);
            TheAnalyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(TheVersion, STOP_WORDS);
            TheStringBuilder = new StringBuilder();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // free managed resources
                if (TheAnalyzer != null)
                {
                    TheAnalyzer.Dispose();
                    TheAnalyzer = null;
                }
                if (TheAnalyzerWithGermanStopWords != null)
                {
                    TheAnalyzerWithGermanStopWords.Dispose();
                    TheAnalyzerWithGermanStopWords = null;
                }
            }
        }

        /// <summary>
        /// to analyzeString
        /// </summary>
        /// <param name="inString">the String to analyze</param>
        /// <returns>the analyzed String</returns>
        public String analyzeString(ref String inString)
        {
            TheStringBuilder.Clear();
            
            using (StringReader TheStringReader = new StringReader(inString))
            {
                TheTokenStream = TheAnalyzer.TokenStream(null, TheStringReader);
                TermAttribute TermAttribute = TheTokenStream.AddAttribute(typeof(TermAttribute)) as TermAttribute;
                while (TheTokenStream.IncrementToken())
                {
                    String TheTempString = TermAttribute.Term();
                    TheStringBuilder.AppendFormat("{0} ", TheTempString);
                }
            }
            return TheStringBuilder.ToString();
        }

        /// <summary>
        /// to analyzeString with German stopword List
        /// </summary>
        /// <param name="inString">the String to analyze</param>
        /// <returns>the analyzed String</returns>
        public String analyzeStringWithGermanStopwordlist(ref String inString)
        {
            TheStringBuilder.Clear();
            using (StringReader TheStringReader = new StringReader(inString))
            {
                TheTokenStream = TheAnalyzerWithGermanStopWords.TokenStream(null, TheStringReader);
                TermAttribute TermAttribute = TheTokenStream.AddAttribute(typeof(TermAttribute)) as TermAttribute;
                while (TheTokenStream.IncrementToken())
                {
                    String TheTempString = TermAttribute.Term();
                    TheStringBuilder.AppendFormat("{0} ", TheTempString);
                }
            }
            return TheStringBuilder.ToString();
        }

        /// <summary>
        /// most used german stopp words
        /// </summary>
        private void buildStoppWordList()
        {
            STOP_WORDS.Add("class");
            STOP_WORDS.Add("id");
            STOP_WORDS.Add("date");
            STOP_WORDS.Add("data");
            STOP_WORDS.Add("?");
            STOP_WORDS.Add("0");
            STOP_WORDS.Add("1");
            STOP_WORDS.Add("2");
            STOP_WORDS.Add("3");
            STOP_WORDS.Add("4");
            STOP_WORDS.Add("5");
            STOP_WORDS.Add("6");
            STOP_WORDS.Add("7");
            STOP_WORDS.Add("8");
            STOP_WORDS.Add("9");

            foreach (String TheTempString in STOP_WORDS)
            {
                STOP_WORDS_GERMAN.Add(TheTempString);
            }

            STOP_WORDS_GERMAN.Add("einer");
            STOP_WORDS_GERMAN.Add("eine");
            STOP_WORDS_GERMAN.Add("eines");
            STOP_WORDS_GERMAN.Add("einem");
            STOP_WORDS_GERMAN.Add("einen");
            STOP_WORDS_GERMAN.Add("der");
            STOP_WORDS_GERMAN.Add("die");
            STOP_WORDS_GERMAN.Add("das");
            STOP_WORDS_GERMAN.Add("dass");
            STOP_WORDS_GERMAN.Add("du");
            STOP_WORDS_GERMAN.Add("er");
            STOP_WORDS_GERMAN.Add("sie");
            STOP_WORDS_GERMAN.Add("es");
            STOP_WORDS_GERMAN.Add("was");
            STOP_WORDS_GERMAN.Add("wer");
            STOP_WORDS_GERMAN.Add("wie");
            STOP_WORDS_GERMAN.Add("wir");
            STOP_WORDS_GERMAN.Add("und");
            STOP_WORDS_GERMAN.Add("oder");
            STOP_WORDS_GERMAN.Add("ohne");
            STOP_WORDS_GERMAN.Add("mit");
            STOP_WORDS_GERMAN.Add("am");
            STOP_WORDS_GERMAN.Add("im");
            STOP_WORDS_GERMAN.Add("in");
            STOP_WORDS_GERMAN.Add("aus");
            STOP_WORDS_GERMAN.Add("auf");
            STOP_WORDS_GERMAN.Add("ist");
            STOP_WORDS_GERMAN.Add("sein");
            STOP_WORDS_GERMAN.Add("war");
            STOP_WORDS_GERMAN.Add("wird");
            STOP_WORDS_GERMAN.Add("ihr");
            STOP_WORDS_GERMAN.Add("ihre");
            STOP_WORDS_GERMAN.Add("ihres");
            STOP_WORDS_GERMAN.Add("als");
            STOP_WORDS_GERMAN.Add("von");
            STOP_WORDS_GERMAN.Add("dich");
            STOP_WORDS_GERMAN.Add("dir");
            STOP_WORDS_GERMAN.Add("mich");
            STOP_WORDS_GERMAN.Add("mir");
            STOP_WORDS_GERMAN.Add("mein");
            STOP_WORDS_GERMAN.Add("klein");
            STOP_WORDS_GERMAN.Add("durch");
            STOP_WORDS_GERMAN.Add("wegen");
            STOP_WORDS_GERMAN.Add("fÃ¼r");
        }
    }
}